package com.android.wake_up_enable_hotspot;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class WifiApUtil {

    public static final String TAG = WifiApUtil.class.getSimpleName();
    
    // wifi热点开关
    public static void setWifiApEnabled(boolean isEnable) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
            Log.d(TAG, "安卓6.0及以下，使用setWifiApEnabled反射，开启关闭热点。");

            WifiManager mWifiManager = (WifiManager) MyApplication.applicationContext.getSystemService(Context.WIFI_SERVICE);

            try {
                Method methodWifiApState = mWifiManager.getClass().getMethod("getWifiApState");
                int getWifiApState = (Integer) methodWifiApState.invoke(mWifiManager);

                // 获取属性值
                Field field = mWifiManager.getClass().getDeclaredField("WIFI_AP_STATE_ENABLED");
                int value = (int) field.get(mWifiManager);

                if (isEnable) {
                    if (getWifiApState == value) {
                        Log.d(TAG, "热点已开启");
                        return;
                    }
                }

                Method methodWifiApConfiguration = mWifiManager.getClass().getMethod("getWifiApConfiguration");
                WifiConfiguration mWifiConfiguration = (WifiConfiguration) methodWifiApConfiguration.invoke(mWifiManager);

                Method methodWifiApEnabled = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
                boolean flag = (Boolean) methodWifiApEnabled.invoke(mWifiManager, mWifiConfiguration, isEnable);
                Log.d(TAG, "热点" + (isEnable ? "开启" : "关闭") + "操作状态：" + flag);

                if (flag) {
                    if (isEnable) {
                        Log.d(TAG, "热点成功开启");
                    }
                }

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            return;
        }

        TestOreoWifiManager mTestOreoWifiManager = new TestOreoWifiManager();
        if (isEnable) {
            Log.d(TAG, "安卓7.0以上，以proxy实现，开启关闭热点。");

            TestOreoWifiManager.TestOnStartTetheringCallback mCallback = new TestOreoWifiManager.TestOnStartTetheringCallback() {
                @Override
                public void onTetheringStarted() {
                    Log.d(TAG, "热点开启状态：" + "onTetheringStarted");
                }

                @Override
                public void onTetheringFailed() {
                    Log.d(TAG, "热点开启操作状态：" + "onTetheringFailed。");
                    Log.d(TAG, "onTetheringFailed");
                }
            };
            mTestOreoWifiManager.startTethering(mCallback);

        } else {
            mTestOreoWifiManager.stopTethering();
        }
    }

    // 关闭热点
    public static void closeWifiAp() {
        WifiApUtil.setWifiApEnabled(false);
    }

    public static boolean isWifiApOpen(Context context) {
        try {
            WifiManager manager = (WifiManager) context.getSystemService("wifi");
            Method method = manager.getClass().getDeclaredMethod("getWifiApState", new Class[0]);
            int state = ((Integer) method.invoke(manager, new Object[0])).intValue();
            Field field = manager.getClass().getDeclaredField("WIFI_AP_STATE_ENABLED");
            int value = ((Integer) field.get(manager)).intValue();
            if (state != value) {
                return false;
            }
            return true;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        } catch (NoSuchFieldException e2) {
            e2.printStackTrace();
            return false;
        } catch (NoSuchMethodException e3) {
            e3.printStackTrace();
            return false;
        } catch (InvocationTargetException e4) {
            e4.printStackTrace();
            return false;
        }
    }


}
